package servlet;

import pojo.Booking;
import service.BookingService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PipedWriter;
import java.io.PrintWriter;

@WebServlet(name = "AddServlet", value = "/AddServlet")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String userName = request.getParameter("userName");
        String cardId = request.getParameter("cardId");
        String bookingDate = request.getParameter("bookingDate");
        Booking booking  = new Booking();
        booking.setUserName(userName);
        booking.setCardId(cardId);
        booking.setStatus(0);
        booking.setBookingDate(bookingDate);
        BookingService service = new BookingService();
        int result = service.add(booking);
        if (result>0){
            out.print("<script>alert('预约成功');location.href='ListServlet'</script>");
        } else {
            out.print("<script>alert('预约失败');location.href='ListServlet'</script>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
